//-------------------------------------------------------------------------------------------------
// <copyright file="SA1200.cs" company="OfficeClip LLC">
// Copyright (c) OfficeClip LLC.  All rights reserved.
// </copyright>
//-------------------------------------------------------------------------------------------------

namespace OfficeClip.OpenSource.StyleFix.Drivers
{
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Text.RegularExpressions;

    public class SA1200
    {
        public static void Run(ref List<string> outputFileLines)
        {
            // Remove the real namespace from the bottom
            string namespaceLine = string.Empty;
            for (int i = 0; i < outputFileLines.Count - 1; i++)
            {
                if (Regex.Match(outputFileLines[i], @"\busing\b\s*[^\(](\w*\.)*\w*\;").Success)
                {
                    outputFileLines[i] = "    " + outputFileLines[i];
                    continue;
                }

                if (outputFileLines[i].Trim().StartsWith("namespace"))
                {
                    namespaceLine = outputFileLines[i].Trim();
                    outputFileLines.RemoveAt(i);

                    // If there is a blank line remove it
                    if (outputFileLines[i].Trim() == string.Empty)
                    {
                        outputFileLines.RemoveAt(i);
                    }

                    if (outputFileLines[i] == "{")
                    {
                        outputFileLines.RemoveAt(i);
                    }

                    break;
                }
            }

            // now add the namespace to the top
            for (int i = 0; i < outputFileLines.Count - 1; i++)
            {
                if (outputFileLines[i].TrimStart().StartsWith("using"))
                {
                    // Add the namespace around it
                    int insertPosition = i;
                    outputFileLines.Insert(insertPosition++, namespaceLine);
                    outputFileLines.Insert(insertPosition, "{");
                    break;
                }
            }
        }
    }
}
